var vue = new Vue({
    el: "#main",
    data() {
        return {
            tabIndex: 1, //tab值
            id: 1, //网格的id
            user_id: 1, //网格的user_id
            list: [],
            announcementList: [], //公告列表
            total: 0, //总人数
            care_rate: 0, //走访率
            searchVal: '',
            searchLoading: false,
            week: '', //关爱的第几周
            year: '', //年份
            yearArr: [], //年份数组
        }
    },

    created() {
        this.id = getRequest().id
        this.user_id = getRequest().user_id
        this.getTime()
    },
    mounted() {
        this.getList()
        window.addEventListener("visibilitychange", () => {
            if (document.hidden) {

            } else {
                this.getList()
            }
        })
    },
    methods: {
        // 获取时间
        getTime() {
            let time = new Date()
            // this.year = time.getFullYear()
            // let firstDayOfYear = new Date(time.getFullYear(), 0, 1)
            // const dayOfWeek = firstDayOfYear.getDay()
            // let spendDay = 1;
            // if (dayOfWeek != 0) {
            //     spendDay = 7 - dayOfWeek + 1;
            // }
            // firstDayOfYear = new Date(time.getFullYear(), 0, spendDay);
            // const d = Math.ceil((time.valueOf() - firstDayOfYear.valueOf()) / 86400000);
            // 获取当前周数
            // this.week = Math.ceil(d / 7);

            // let currentMonth = time.getMonth() + 1;
            // if (currentMonth >= 1 && currentMonth <= 3) {
            //     this.rateIndex = 0;
            // } else if (currentMonth >= 4 && currentMonth <= 6) {
            //     this.rateIndex = 1;
            // } else if (currentMonth >= 7 && currentMonth <= 9) {
            //     this.rateIndex = 2;
            // } else {
            //     this.rateIndex = 3;
            // }
            let _year = time.getFullYear() - 2023
            for (let i = 0; i <= _year; i++) {
                this.yearArr.push(time.getFullYear() - i)
            }
        },
        // 改变关爱走访率年
        changeYear() {
            ajax({
                url: formal + `/sick-man/get-sick-man?dept_id=${this.id}&user_id=${this.user_id}&frequency=${this.tabIndex}&keywords=${this.searchVal}&year=${this.year}&week=${this.week}`,
                type: 'GET',
                success: (res) => {
                    this.care_rate = res.data.care_rate
                }
            })
        },
        // 改变关爱走访率周
        changeRate(val) {
            if (val == -1) {
                if (this.week == 1) {
                    vant.Toast('已经是本年的第一周');
                    return
                }
            } else {
                if (this.week == 52) {
                    vant.Toast('已经是本年的最后一周');
                    return
                }
            }
            this.week = this.week + val
            ajax({
                url: formal + `/sick-man/get-sick-man?dept_id=${this.id}&user_id=${this.user_id}&frequency=${this.tabIndex}&keywords=${this.searchVal}&year=${this.year}&week=${this.week}`,
                type: 'GET',
                success: (res) => {
                    this.care_rate = res.data.care_rate
                }
            })
        },
        // 获取老人列表
        getList() {
            ajax({
                url: formal + `/sick-man/get-sick-man?dept_id=${this.id}&user_id=${this.user_id}&frequency=${this.tabIndex}&keywords=${this.searchVal}&year=${this.year}&week=${this.week}`,
                type: 'GET',
                success: (res) => {
                    this.list = res.data.list
                    this.total = res.data.total
                    this.care_rate = res.data.care_rate
                    this.searchLoading = false
                    this.year = res.data.year * 1
                    this.week = res.data.week * 1
                    if (this.announcementList.length == 0) {
                        this.announcementList = res.data.noMan
                        if (this.announcementList.length >= 1) {
                            setTimeout(() => {
                                new Swiper(".mySwiper", {
                                    direction: "vertical",
                                    loop: true,
                                    autoplay: {
                                        delay: 1500, //自动切换的时间间隔，单位ms
                                        disableOnInteraction: false //用户操作swiper之后，是否禁止autoplay
                                    },
                                });
                            }, 500)
                        }
                    }
                }
            })
        },
        // 切换tab
        changeTab(value) {
            this.tabIndex = value
            this.getList()
        },
        searchChange() {
            if (this.searchVal == '') {
                this.getList()
            }
        },
        search() {
            if (this.searchVal == '') return;
            this.searchLoading = true
            this.getList()
        },
        // 去添加
        add() {
            location.href = '../addCare.html?id=' + this.id + '&user_id=' + this.user_id
        },
        // 点击公告去关爱老人
        goAnnouncementCareRecords(e) {
            let frequency = e.target.dataset.frequency
            let last_care_status = e.target.dataset.last_care_status
            let last_care_id = e.target.dataset.last_care_id
            if (e.target.dataset.uuid) {
                location.href = '../care.html?id=' + e.target.dataset.uuid + '&type=' + frequency + '&last_care_status=' + last_care_status + '&last_care_id=' + last_care_id
            }
        },
        // 去关爱老人记录或关爱老人
        goCareRecords(e, id, last_care_id, last_care_status) {
            if (e.target.dataset.type == 'care') {
                location.href = '../care.html?id=' + id + '&type=' + this.tabIndex + '&last_care_id=' + last_care_id + '&status=' + last_care_status
            } else {
                location.href = '../careRecords.html?id=' + id
            }
        },
    }
})